//页面通用初始化
function pageInit(pageName) {
  //页面路径所需 页面当前处于哪个步骤
  $("#nav-bar").scope().isFix = false;
  // 获取pageIndex
  var pagepages = isOffline ? pagesOffline : pages
  var pageIndex = -1;
  for (var i in pagepages) {
    var strs = pagepages[i].path.split('/');
    if (strs[strs.length - 1] == pageName) {
      pageIndex = i;
      break;
    }
  }

  $("#path-bar").scope().pageIndex = pageIndex;
  $("#path-bar-bg").scope().pageIndex = pageIndex;
  //tooltip初始化
  $('[data-toggle="tooltip"]').tooltip();
  //移除阻止刷新
  $(window).unbind('beforeunload');
}

function menuInit($scope, $http) {
  //菜单的初始化
  $scope.menus = isOffline ? menusOffline : menus;
  $http.get('/deploy/param/menus/get').then(function (response) {
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        var dMenu = message.data;
        for (var i in dMenu)
          for (var j in $scope.menus[0].children)
            if ('#!/' + $scope.menus[0].children[j].path == dMenu[i].path)
              $scope.menus[0].children[j].exist = dMenu[i].exist;
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });
}

//参数配置的通用方法
//获取下个页面
function getNext(menuIndex) {
  var index;
  for (var i = 0; i < menus[0].children.length; i++) {
    if (menus[0].children[i].menuIndex == menuIndex) {
      index = ++i;
      break;
    }
  }
  if (index < menus[0].children.length) {
    var menu = checkThisNext(menus[0].children[index].menuIndex);
    return menu.path;
  } else {
    return 'dbset'
  }
}
function checkThisNext(menuIndex) {
  for (var i = 0; i < menus[0].children.length; i++) {
    if (menus[0].children[i].menuIndex == menuIndex) {
      if (menus[0].children[i].exist == false) {
        if (i + 1 < menus[0].children.length)
          return checkThisNext(menus[0].children[++i].menuIndex);
        else
          return {
            name: '数据库配置',
            menuIndex: 2.1,
            path: 'dbset'
          };
      } else {
        return menus[0].children[i];
      }
    }
  }
}
//获取上个页面
function getLast(menuIndex) {
  var index;
  for (var i = 0; i < menus[0].children.length; i++) {
    if (menus[0].children[i].menuIndex == menuIndex) {
      index = --i;
      break;
    }
  }
  if (index > 0) {
    var menu = checkThisLast(menus[0].children[index].menuIndex);
    return menu.path;
  } else {
    return 'module'
  }
}
function checkThisLast(menuIndex) {
  for (var i = 0; i < menus[0].children.length; i++) {
    if (menus[0].children[i].menuIndex == menuIndex) {
      if (menus[0].children[i].exist == false) {
        if (i - 1 > 0)
          return checkThisLast(menus[0].children[--i].menuIndex);
        else
          return {
            name: '系统模块配置',
            menuIndex: 1.1,
            path: 'module'
          };
      } else {
        return menus[0].children[i];
      }
    }
  }
}
//获取配置项序号
function getIndexOfConfig($scope, configName, appName) {
  for (var i in $scope.configs) {
    if ($scope.configs[i].name == configName) {
      if (appName) {
        for (var j in $scope.configs[i].appList) {
          if ($scope.configs[i].appList[j].name == appName) {
            return [i, j];
          }
        }
      } else {
        return [i];
      }
    }
  }
}
function getIndexOfConfig2(configs, configName, appName) {
  for (var i in configs) {
    if (configs[i].name == configName) {
      if (appName) {
        for (var j in configs[i].appList) {
          if (configs[i].appList[j].name == appName) {
            return [i, j];
          }
        }
      } else {
        return [i];
      }
    }
  }
}
//删除配置项
function deleteConfigs($scope, configNames, appName) {
  for (var m in configNames) {
    for (var i in $scope.configs) {
      if ($scope.configs[i].name == configNames[m]) {
        if (appName) {
          for (var j in $scope.configs[i].appList) {
            if ($scope.configs[i].appList[j].name == appName) {
              $scope.configs[i].appList.splice(j, 1);
            }
          }
        } else {
          $scope.configs.splice(i, 1);
        }
      }
    }
  }
}

function configInit($scope, $http, url, $location, cb) {
  $scope.hasChange = false;
  // 阻止刷新
  $(window).bind('beforeunload', function () {
    if ($scope.hasChange)
      return '离开该页面，修改的配置都将还原，确认离开？';
  });
  //初始化
  $http.get('/deploy/param/' + url + '/init').then(function (response) {
    if (response.status == 200) {
      var message = response.data;
      if (message.code == 0) {
        $scope.configs = message.data;
        setTimeout(function () {
          console.log($('[data-toggle="tooltip"]'));
          $('[data-toggle="tooltip"]').tooltip();
        }, 300);
        if (cb)
          cb();
      } else {
        $.showNotice(message.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });

  //上一页
  $scope.goBack = function () {
    $location.path(getLast($scope.menuIndex));
  };

  // 检测表单
  $scope.checkForm = function () {
    for (var i in $scope.configs) {
      if ($scope.configs[i].isNull) {
        $.showNotice('请完成表单表单填写', 'warning');
        return false;
      }
      for (var j in $scope.configs[i].appList) {
        if ($scope.configs[i].appList[j].isNull) {
          $.showNotice('请完成表单表单填写', 'warning');
          return false;
        }
      }
    }
    return true;
  }

  //提交
  $scope.submitFnc = function () {
    // 检测表单
    if (!$scope.checkForm()) return;
    $scope.loadingNext = true;
    $http.post('/deploy/param/' + url + '/submit', $scope.configs).then(function (response) {
      $scope.loadingNext = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var dMenu = message.data;
          for (var i in dMenu)
            for (var j in menus[0].children)
              if ('#!/' + menus[0].children[j].path == dMenu[i].path)
                menus[0].children[j].exist = dMenu[i].exist;
          $scope.hasChange = false;
          $location.path(getNext($scope.menuIndex));
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //保存
  $scope.saveFnc = function () {
    $http.post('/deploy/param/' + url + '/submit', $scope.configs).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var dMenu = message.data;
          for (var i in dMenu)
            for (var j in menus[0].children)
              if ('#!/' + menus[0].children[j].path == dMenu[i].path)
                menus[0].children[j].exist = dMenu[i].exist;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  };

  //文本校验
  $scope.blur = function (app, flag, isNumber) {
    $scope.hasChange = true;

    if (isNumber) {
      app.value = app.value.replace(/\D/g, '');
      var length = app.value.length;
      for (var i = 0; i < app.value.length; i++) {
        if (app.value.charAt(i) == '0') {
          app.value = app.value.substr(i + 1);
          i--;
          length--;
        } else {
          break;
        }
      }
    }
    if (flag)
      if (!app.value || Trim(app.value, true) == '') {
        app.isNull = true;
      } else {
        app.isNull = false;
      }
  }

  // 
  $scope.changeRadio = function () {
    // console.log("hasChange");
    $scope.hasChange = true;
  }
}

//去空格
function Trim(str, is_global) {
  var result;
  result = str.replace(/(^\s+)|(\s+$)/g, "");
  if (is_global)
    result = result.replace(/\s/g, "");
  return result;
}

$(function () {
  //左导航按钮滚动固定
  $(document).scroll(function () {
    var top = $(window).scrollTop();

    if (top >= 50) {
      $("#path-bar").addClass('path-bar-fixed');
      //$(".menu").addClass('menu-fixed');
    }
    else {
      $("#path-bar").removeClass('path-bar-fixed');
      //$(".menu").removeClass('menu-fixed');
    }

  });
});